package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.util.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @author study
 * @version 1.0
 * @date 2021/5/6 15:53
 */
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    //查询医院设置表所以信息
    @ApiOperation(value = "获取所有医院设置信息")
    @GetMapping("findAll")
    public Result findAllHospital() {
        final List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "删除医院设置")
    @DeleteMapping("{id}")
    public Result removeHosptical(@PathVariable Long id) {
        final boolean b = hospitalSetService.removeById(id);
        if (b) {
            return Result.ok();
        }
        return Result.fail();
    }

    //3 条件查询带分页|创建VO类，封装条件值，
    @ApiOperation(value = "分页查询")
    @GetMapping("findPage/{current}/{limit}")
    public Result findPageHospSet(
            @PathVariable long current,
            @PathVariable long limit,
            @RequestBody(required = false) HospitalQueryVo hospitalQueryVo) {
        //创建 page对象
        final Page<HospitalSet> page = new Page<>(current, limit);
        //构建条件
        final QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(hospitalQueryVo.getHosname())) {
            wrapper.like("hostname", hospitalQueryVo.getHosname());
        }
        wrapper.eq(!StringUtils.isEmpty(hospitalQueryVo.getHoscode()), "hoscode", hospitalQueryVo.getHoscode());
        final Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, wrapper);
        //true
        System.out.println(hospitalSetPage == page);
        return Result.ok(hospitalSetPage);
    }


    // 4 添加 医院设置
    @PostMapping("saveHospitalSet")
    public Result save(@RequestBody HospitalSet hospitalSet) {

        //设置状态 1 使用，0 不能使用
        hospitalSet.setStatus(1);
        final Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        final boolean save = hospitalSetService.save(hospitalSet);
        if (save) {
            return Result.ok();
        }
        return Result.fail();
    }

    // 5 根据ID 设置
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id) {
        final HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    // 6
    @PostMapping("updateHospitalSet")
    public Result updateHospSet(@RequestBody HospitalSet hospitalSet) {

        final boolean update = hospitalSetService.updateById(hospitalSet);
        if (update) {
            return Result.ok();
        }
        return Result.fail();
    }

    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }

    //医院设置锁定 解锁
    @ApiOperation("医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospSet(
            @PathVariable Long id,
            @PathVariable int status) {

        HospitalSet hospital = new HospitalSet();
        hospital.setId(id);
        hospital.setStatus(status);
        hospitalSetService.updateById(hospital);
        return Result.ok();
    }
}
